﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mapeo;
using BE;
using NHibernate;
using MySql.Data.MySqlClient;
using System.Data;
using NHibernate.Criterion;

namespace DAL
{
    public class Empleado_DAL : Controlador
    {
        public void Prueba() {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        Empleado empl = new Empleado();
                        empl.ID = "44374558";
                        empl.ApellidoPaterno = "CARH";
                        empl.ApellidoMaterno = "MONTENEGRO";
                        empl.Nombres= "ROSS";
                        empl.Area = "SISTEMAS";
                        empl.EsTrabajador = true;
                        empl.EsAlumno = false;
                        empl.EsCliente = false;


                        Sesion.Save(empl);
                        Sesion.Flush();
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        
        }
        public void InsertarEmpleado(Empleado empleado)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        empleado.EsTrabajador = true;
                        Sesion.Save(empleado);
                        Sesion.Flush();
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }
        
        public void ActualizarEmpleado(Empleado empleado)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        empleado.EsTrabajador = true;
                        Sesion.Update(empleado);
                        //Sesion.Flush();
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }

        public void ModificarEmpleado(Empleado empleado)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {
                        Sesion.Update(empleado);
                        Sesion.Flush();
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }

        }


        public void Eliminar(Int32 id)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    try
                    {

                        Empleado empleado = (Empleado)Sesion.CreateCriteria("Empleado").Add(Restrictions.Eq("IDSN", Convert.ToInt32(id))).UniqueResult();
                        Sesion.Delete(empleado);
                        Sesion.Flush();
                        Trans.Commit();
                    }
                    catch (Exception)
                    {
                        Trans.Rollback();
                        throw;
                    }
                }
            }
        }


        public DataTable listado(String Codigo, String Nombre)
        {
            using (ISession Sesion = m_SessionFactory.OpenSession())
            {
                using (ITransaction Trans = Sesion.BeginTransaction())
                {
                    MySqlCommand SqlCmd = new MySqlCommand();
                    SqlCmd.Connection = (MySqlConnection)Sesion.Connection;
                    Trans.Enlist(SqlCmd);
                    SqlCmd.CommandText = "sp_ObtenerEmpleados";
                    SqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                    SqlCmd.Parameters.AddWithValue("@in_codigo", Codigo);
                    SqlCmd.Parameters.AddWithValue("@in_nombre", Nombre);
                    SqlCmd.ExecuteNonQuery();
                    MySqlDataAdapter da = new MySqlDataAdapter(SqlCmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    Trans.Commit();
                    return dt;
                }
            }
        }


        public Empleado obtenerEmpleadoPorId(Int32 ID)
        {

               try
               {
                   using (ISession Sesion = m_SessionFactory.OpenSession())
                   {
                       Object Obj = (Object)Sesion.CreateCriteria("Empleado").Add(Restrictions.Eq("IDSN", ID)).UniqueResult();

                       return (Empleado)Obj;
                   }
               }
               catch (Exception ex)
               {

                   throw ex;
               }

        }



        public List<Cargo> ObtenerCargos()
        {
            try
            {
                using (ISession Sesion = m_SessionFactory.OpenSession())
                {
                    ICriteria criteria = Sesion.CreateCriteria("Cargo")
                        .AddOrder(Order.Asc("descripcion"));
                    //criteria.SetMaxResults(100);
                    List<Cargo> listado = (List<Cargo>)criteria.List<Cargo>();
                    return listado;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<Programas> ObtenerProgramas()
        {
            try
            {
                using (ISession Sesion = m_SessionFactory.OpenSession())
                {
                    ICriteria criteria = Sesion.CreateCriteria("Programas")
                        .AddOrder(Order.Asc("Descripcion"));
                    //criteria.SetMaxResults(100);
                    List<Programas> listado = (List<Programas>)criteria.List<Programas>();
                    return listado;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
